package com.yuchen.learning.controller;

import com.yuchen.learning.common.Result;
import com.yuchen.learning.entity.User;
import com.yuchen.learning.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result<?> register(@RequestBody User user) {
        boolean ok = userService.register(user);
        return ok ? Result.success(null) : Result.error("用户名已存在");
    }

    @PostMapping("/login")
    public Result<?> login(@RequestBody Map<String, String> map) {
        String username = map.get("username");
        String password = map.get("password");
        User user = userService.login(username, password);
        return user != null ? Result.success(user) : Result.error("用户名或密码错误");
    }
}